<template src="./template.html"></template>

<script>
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG } from 'common/consts'
import { COL_INDEXS_TRAINEE, COL_INDEXS_GROUP } from './consts'
import { getStatLessonTrainee, getStatLessonGroup, getFlushStatLessonTrainee, getFlushStatLessonGroup } from '../models'
import { getCourseStuGroup } from '../../trainee/models'

export default {
  mixins: [ResizeMixin],
  data() {
    return {
      COL_INDEXS_TRAINEE,
      COL_INDEXS_GROUP,
      isPageLoading: false,

      statType: 'trainee',
      groupList: [],

      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        q: '',
        exerciseType: '',
        groupId: '',
        lessonId: '',
        passingState: ''
      },
    };
  },
  computed: {
    courseId() {
      return this.$route.params.courseId
    },
    lessonId() {
      return this.$route.params.lessonId
    },
    styName() {
      return this.$route.query.name
    }
  },

  methods: {
    async $flush() {
      const params = {
        courseId: this.courseId,
        lessonId: this.lessonId
      };
      let func = getFlushStatLessonTrainee
      if (this.statType === 'group') {
        func = getFlushStatLessonGroup
      }
      try {
        await func(params);
        this.$search()
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || '刷新异常！')
      }
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
        courseId: this.courseId,
        lessonId: this.lessonId
      };
      let func = getStatLessonTrainee
      if (this.statType === 'group') {
        func = getStatLessonGroup
      }
      try {
        const { data } = await func(params);
        this.tableList = data.list || []
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
    async $getCourseStuGroup() {
      this.isPageLoading = true;
      const params = {
        limit: 99999,
        page: 1,
        courseId: this.courseId,
        lessonId: this.lessonId
      };
      try {
        const { data } = await getCourseStuGroup(params);
        this.groupList = data.list || []
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取列表出错')
      }
    }
  },
  created() {
    this.$getTableList();
    this.$getCourseStuGroup()
  },
};
</script>
